import 'dart:convert' show json;

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class MahjongRule {
  MahjongRule({this.userCount, this.numberGame, this.rules, this.turns});

  MahjongRule.init({this.userCount, this.numberGame, this.rules, this.turns});

  factory MahjongRule.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }
    final List<String> rules = jsonRes['rules'] is List ? <String>[] : null;
    if (rules != null) {
      for (final dynamic item in jsonRes['rules']) {
        if (item != null) {
          rules.add(asT<String>(item));
        }
      }
    }

    return MahjongRule(
      userCount: asT<int>(jsonRes['userCount']),
      turns: asT<int>(jsonRes['turns']),
      numberGame: asT<int>(jsonRes['numberGame']),
      rules: rules,
    );
  }

  int userCount;
  int numberGame;
  int turns;
  List<String> rules;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'userCount': userCount,
        'turns': turns,
        'numberGame': numberGame,
        'rules': rules,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
